<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>高相似度论文管理 - 论文题目查重系统</title>
    <link rel="icon" type="image/x-icon" href="/static/favicon.ico">
    <link rel="stylesheet" href="/static/admin_styles.css">
    <style>
        :root {
            --primary-color: #9b59b6;
            --secondary-color: #2c3e50;
            --success-color: #27ae60;
            --warning-color: #f39c12;
            --danger-color: #e74c3c;
            --light-bg: #f8f9fa;
            --dark-bg: #2c3e50;
            --text-color: #333;
            --text-light: #f8f9fa;
            --border-radius: 8px;
            --box-shadow: 0 10px 30px rgba(0,0,0,0.15);
            --transition: all 0.3s ease;
        }

        /* 暗色主题变量 */
        .dark-theme {
            --primary-color: #af7ac5;
            --secondary-color: #85929e;
            --success-color: #58d68d;
            --warning-color: #f7dc6f;
            --danger-color: #ec7063;
            --light-bg: #34495e;
            --dark-bg: #2c3e50;
            --text-color: #ecf0f1;
            --text-light: #ecf0f1;
        }

        body {
            font-family: "Microsoft YaHei", "SimSun", sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            margin: 0;
            padding: 20px;
            color: var(--text-color);
            min-height: 100vh;
            transition: var(--transition);
        }

        body.dark-mode {
            background: linear-gradient(135deg, #1a2330 0%, #2c3e50 100%);
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            background-color: white;
            padding: 30px;
            border-radius: var(--border-radius);
            box-shadow: var(--box-shadow);
            transition: var(--transition);
            position: relative;
        }

        body.dark-mode .container {
            background-color: var(--dark-bg);
        }

        h1 {
            text-align: center;
            color: var(--secondary-color);
            margin-bottom: 30px;
            font-size: 28px;
            position: relative;
        }

        body.dark-mode h1 {
            color: #ecf0f1;
        }

        h1:after {
            content: '';
            display: block;
            width: 80px;
            height: 4px;
            background: linear-gradient(to right, var(--primary-color), var(--success-color));
            margin: 10px auto;
            border-radius: 2px;
        }

        .controls {
            display: flex;
            justify-content: space-between;
            margin-bottom: 25px;
            gap: 15px;
            flex-wrap: wrap;
        }

        .back-btn {
            background-color: #95a5a6;
            color: white;
            border: none;
            padding: 12px 20px;
            font-size: 16px;
            border-radius: var(--border-radius);
            cursor: pointer;
            transition: var(--transition);
            font-weight: bold;
            box-shadow: 0 4px 6px rgba(50, 50, 93, 0.11), 0 1px 3px rgba(0, 0, 0, 0.08);
            display: flex;
            align-items: center;
        }

        .back-btn:hover {
            background-color: #7f8c8d;
            transform: translateY(-2px);
        }

        .search-group {
            display: flex;
            flex: 1;
            gap: 10px;
            min-width: 300px;
        }

        .search-group input {
            flex: 1;
            padding: 15px;
            border: 2px solid #e1e5eb;
            border-radius: var(--border-radius);
            font-size: 16px;
            box-sizing: border-box;
            transition: border-color 0.3s;
            background-color: white;
            color: var(--text-color);
        }

        body.dark-mode .search-group input {
            background-color: #34495e;
            border-color: #3d566e;
            color: var(--text-light);
        }

        .search-group input:focus {
            border-color: var(--primary-color);
            outline: none;
        }

        .search-group button {
            background-color: var(--primary-color);
            color: white;
            border: none;
            padding: 12px 20px;
            font-size: 16px;
            border-radius: var(--border-radius);
            cursor: pointer;
            transition: var(--transition);
            font-weight: bold;
        }

        .search-group button:hover {
            background-color: #8e44ad;
            transform: translateY(-2px);
        }

        .clear-search-btn {
            background-color: var(--danger-color);
            color: white;
            border: none;
            padding: 12px 20px;
            font-size: 16px;
            border-radius: var(--border-radius);
            cursor: pointer;
            transition: var(--transition);
            font-weight: bold;
        }

        .clear-search-btn:hover {
            background-color: #c0392b;
            transform: translateY(-2px);
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.05);
            border-radius: var(--border-radius);
            overflow: hidden;
        }

        th, td {
            padding: 15px;
            text-align: left;
            border-bottom: 1px solid #eee;
        }

        body.dark-mode th, 
        body.dark-mode td {
            border-bottom-color: #3d566e;
        }

        th {
            background-color: var(--light-bg);
            font-weight: bold;
            color: var(--secondary-color);
            text-transform: uppercase;
            font-size: 14px;
            letter-spacing: 0.5px;
        }

        body.dark-mode th {
            background-color: #34495e;
            color: #ecf0f1;
        }

        tr:nth-child(even) {
            background-color: var(--light-bg);
        }

        body.dark-mode tr:nth-child(even) {
            background-color: #34495e;
        }

        tr:hover {
            background-color: #e3f2fd;
        }

        body.dark-mode tr:hover {
            background-color: #3d566e;
        }

        .delete-btn {
            background-color: var(--danger-color);
            color: white;
            border: none;
            padding: 8px 15px;
            border-radius: 5px;
            cursor: pointer;
            transition: var(--transition);
            font-weight: bold;
        }

        .delete-btn:hover {
            background-color: #c0392b;
            transform: translateY(-2px);
        }

        .edit-btn {
            background-color: var(--warning-color);
            color: white;
            border: none;
            padding: 8px 15px;
            border-radius: 5px;
            cursor: pointer;
            transition: var(--transition);
            font-weight: bold;
            margin-right: 5px;
        }

        .edit-btn:hover {
            background-color: #d68910;
            transform: translateY(-2px);
        }

        .pagination {
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 10px;
            margin-top: 30px;
            flex-wrap: wrap;
        }

        .pagination button {
            background-color: var(--primary-color);
            color: white;
            border: none;
            padding: 10px 15px;
            border-radius: 5px;
            cursor: pointer;
            transition: var(--transition);
            font-weight: bold;
        }

        .pagination button:hover {
            background-color: #8e44ad;
            transform: translateY(-2px);
        }

        .pagination button:disabled {
            background-color: #bdc3c7;
            cursor: not-allowed;
            transform: none;
        }

        body.dark-mode .pagination button:disabled {
            background-color: #566573;
        }

        .pagination input {
            width: 70px;
            padding: 10px;
            border: 2px solid #e1e5eb;
            border-radius: 5px;
            text-align: center;
            font-size: 16px;
            background-color: white;
            color: var(--text-color);
        }

        body.dark-mode .pagination input {
            background-color: #34495e;
            border-color: #3d566e;
            color: var(--text-light);
        }

        .pagination input:focus {
            border-color: var(--primary-color);
            outline: none;
        }

        .no-data {
            text-align: center;
            padding: 40px;
            color: #7f8c8d;
            font-style: italic;
        }

        body.dark-mode .no-data {
            color: #bdc3c7;
        }

        .stats-bar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-top: 25px;
            padding: 15px;
            background: linear-gradient(to right, var(--primary-color), var(--secondary-color));
            border-radius: var(--border-radius);
            color: white;
            font-weight: bold;
            box-shadow: 0 4px 6px rgba(50, 50, 93, 0.11), 0 1px 3px rgba(0, 0, 0, 0.08);
            flex-wrap: wrap;
            gap: 15px;
        }

        .system-info {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-top: 20px;
            padding: 15px;
            background-color: #e3f2fd;
            border-radius: var(--border-radius);
            font-size: 14px;
        }

        body.dark-mode .system-info {
            background-color: #3d566e;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .container {
                padding: 15px;
                margin: 10px;
            }
            
            .controls {
                flex-direction: column;
            }
            
            .search-group {
                width: 100%;
            }
            
            table {
                font-size: 14px;
            }
            
            th, td {
                padding: 10px;
            }
            
            .stats-bar {
                flex-direction: column;
                text-align: center;
            }
        }

        /* 纸飞机图标 */
        .paper-plane-icon {
            display: inline-block;
            width: 24px;
            height: 24px;
            background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="white"><path d="M2 21l21-9L2 3v7l15 2-15 2v7z"/></svg>');
            background-repeat: no-repeat;
            background-position: center;
            background-size: contain;
            margin-right: 8px;
            vertical-align: middle;
        }

        body.dark-mode .paper-plane-icon {
            filter: brightness(0) invert(1);
        }

        .header-icon {
            width: 40px;
            height: 40px;
            background-color: var(--primary-color);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 20px;
        }

        /* 主题切换按钮 */
        .theme-toggle {
            position: absolute;
            top: 20px;
            right: 20px;
            background: var(--light-bg);
            border: none;
            border-radius: 50%;
            width: 40px;
            height: 40px;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            box-shadow: 0 2px 5px rgba(0,0,0,0.2);
            transition: var(--transition);
        }

        body.dark-mode .theme-toggle {
            background: #34495e;
        }

        .theme-toggle:hover {
            transform: scale(1.1);
        }

        .theme-toggle svg {
            width: 20px;
            height: 20px;
            fill: var(--text-color);
        }

        /* 搜索结果高亮 */
        .highlight {
            background-color: yellow;
            padding: 2px 4px;
            border-radius: 3px;
        }

        body.dark-mode .highlight {
            background-color: #f1c40f;
            color: #2c3e50;
        }

        /* 卡片样式 */
        .card {
            border-radius: var(--border-radius);
            background-color: var(--light-bg);
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.05);
            transition: var(--transition);
        }

        body.dark-mode .card {
            background-color: #34495e;
        }

        /* 修改意见模态框 */
        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
        }

        .modal-content {
            background-color: white;
            margin: 10% auto;
            padding: 20px;
            border-radius: var(--border-radius);
            width: 80%;
            max-width: 600px;
            box-shadow: var(--box-shadow);
        }

        body.dark-mode .modal-content {
            background-color: var(--dark-bg);
        }

        .close {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
            cursor: pointer;
        }

        .close:hover,
        .close:focus {
            color: black;
        }

        body.dark-mode .close:hover,
        body.dark-mode .close:focus {
            color: white;
        }

        .modal textarea {
            width: 100%;
            height: 150px;
            padding: 10px;
            border: 2px solid #e1e5eb;
            border-radius: var(--border-radius);
            font-family: "Microsoft YaHei", "SimSun", sans-serif;
            font-size: 16px;
            resize: vertical;
            box-sizing: border-box;
            background-color: white;
            color: var(--text-color);
        }

        body.dark-mode .modal textarea {
            background-color: #34495e;
            border-color: #3d566e;
            color: var(--text-light);
        }

        .modal textarea:focus {
            border-color: var(--primary-color);
            outline: none;
        }

        .modal button {
            margin-top: 15px;
            padding: 10px 20px;
            border: none;
            border-radius: var(--border-radius);
            cursor: pointer;
            font-weight: bold;
            transition: var(--transition);
        }

        .save-btn {
            background-color: var(--success-color);
            color: white;
            float: right;
        }

        .save-btn:hover {
            background-color: #219653;
            transform: translateY(-2px);
        }

        .cancel-btn {
            background-color: #95a5a6;
            color: white;
            margin-right: 10px;
        }

        .cancel-btn:hover {
            background-color: #7f8c8d;
            transform: translateY(-2px);
        }
    </style>
</head>
<body>
    <button class="theme-toggle" id="theme-toggle" title="切换主题">
        <svg viewBox="0 0 24 24">
            <path d="M12,5.5A3.5,3.5 0 0,1 15.5,9A3.5,3.5 0 0,1 12,12.5A3.5,3.5 0 0,1 8.5,9A3.5,3.5 0 0,1 12,5.5M5,8C5.56,8 6.08,8.15 6.53,8.42C6.38,9.85 6.8,11.27 7.66,12.38C7.16,13.34 6.16,14 5,14A3,3 0 0,1 2,11A3,3 0 0,1 5,8M19,8A3,3 0 0,1 22,11A3,3 0 0,1 19,14C17.84,14 16.84,13.34 16.34,12.38C17.2,11.27 17.62,9.85 17.47,8.42C17.92,8.15 18.44,8 19,8M5.5,18.25C5.5,16.18 8.41,14.5 12,14.5C15.59,14.5 18.5,16.18 18.5,18.25V20H5.5V18.25M0,20V18.5C0,17.11 1.89,15.94 4.45,15.6C3.86,16.28 3.5,17.22 3.5,18.25V20H0M24,20H20.5V18.25C20.5,17.22 20.14,16.28 19.55,15.6C22.11,15.94 24,17.11 24,18.5V20Z"></path>
        </svg>
    </button>
    
    <div class="container">
        <div class="controls">
            <button class="back-btn" onclick="window.location.href='/admin'">
                <div class="paper-plane-icon"></div>← 返回管理界面
            </button>
            <div class="search-group">
                <input type="text" id="teacher-search" placeholder="请输入指导教师姓名进行搜索..." value="{{ search_teacher or '' }}">
                <button id="search-btn">搜索</button>
                <button class="clear-search-btn" id="clear-search-btn">清除</button>
            </div>
        </div>
        
        <div class="header-icon">
            <div class="paper-plane-icon"></div>
        </div>
        <h1>高相似度论文题目管理</h1>
        
        <div class="card">
            <table id="high-similarity-table">
                <thead>
                    <tr>
                        <th>指导教师</th>
                        <th>学生姓名</th>
                        <th>学号</th>
                        <th>论文题目</th>
                        <th>录入时间</th>
                        <th>修改意见</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody id="high-similarity-table-body">
                    {% if high_similarity_papers is not none and high_similarity_papers|length > 0 %}
                        {% for paper in high_similarity_papers %}
                        <tr data-id="{{ paper.id }}">
                            <td>{{ paper.teacher }}</td>
                            <td>{{ paper.student }}</td>
                            <td>{{ paper.student_id }}</td>
                            <td>{{ paper.title }}</td>
                            <td>{{ paper.create_time }}</td>
                            <td>{{ paper.suggestion or paper.review_comments or '暂无修改意见' }}</td>
                            <td>
                                <button class="edit-btn" onclick="editReviewComments({{ paper.id }}, '{{ paper.suggestion | escapejs }}')">编辑</button>
                                <button class="delete-btn" onclick="deleteHighSimilarityPaper({{ paper.id }})">删除</button>
                            </td>
                        </tr>
                        {% endfor %}
                    {% else %}
                        <tr>
                            <td colspan="7" class="no-data">暂无数据</td>
                        </tr>
                    {% endif %}
                </tbody>
            </table>
        </div>
        
        <div class="pagination">
            <button id="first-page" {% if current_page <= 1 %}disabled{% endif %} onclick="goToPage(1)">首页</button>
            <button id="prev-page" {% if current_page <= 1 %}disabled{% endif %} onclick="goToPage({{ current_page - 1 }})">上一页</button>
            <span>第 {{ current_page }} 页，共 {{ total_pages }} 页 (总计 {{ total_count }} 条记录)</span>
            <button id="next-page" {% if current_page >= total_pages %}disabled{% endif %} onclick="goToPage({{ current_page + 1 }})">下一页</button>
            <button id="last-page" {% if current_page >= total_pages %}disabled{% endif %} onclick="goToPage({{ total_pages }})">末页</button>
            <span>跳转到第</span>
            <input type="number" id="page-input" min="1" max="{{ total_pages }}" value="{{ current_page }}">
            <span>页</span>
            <button onclick="jumpToPage()">跳转</button>
        </div>
        
        <div class="stats-bar">
            <div>高相似度论文管理面板</div>
            <div>记录总数: {{ total_count }}</div>
            <div>当前页: {{ current_page }} / {{ total_pages }}</div>
        </div>
        
        <div class="system-info">
            <div>高相似度论文管理系统</div>
            <div>状态: <span id="admin-status">就绪</span></div>
        </div>
    </div>

    <!-- 修改意见模态框 -->
    <div id="editModal" class="modal">
        <div class="modal-content">
            <span class="close" onclick="closeModal()">&times;</span>
            <h2>编辑修改意见</h2>
            <textarea id="reviewComments" placeholder="请输入修改意见..."></textarea>
            <button class="cancel-btn" onclick="closeModal()">取消</button>
            <button class="save-btn" onclick="saveReviewComments()">保存</button>
        </div>
    </div>

    <script>
        // 当前编辑的论文ID
        let currentEditId = null;
        
        document.addEventListener('DOMContentLoaded', function() {
            const searchBtn = document.getElementById('search-btn');
            const clearSearchBtn = document.getElementById('clear-search-btn');
            const teacherSearch = document.getElementById('teacher-search');
            const adminStatus = document.getElementById('admin-status');
            const themeToggle = document.getElementById('theme-toggle');
            const body = document.body;
            
            adminStatus.textContent = '就绪';
            
            // 主题切换功能
            const savedTheme = localStorage.getItem('theme');
            if (savedTheme === 'dark') {
                body.classList.add('dark-mode');
            }
            
            themeToggle.addEventListener('click', function() {
                body.classList.toggle('dark-mode');
                
                // 保存主题设置到本地存储
                if (body.classList.contains('dark-mode')) {
                    localStorage.setItem('theme', 'dark');
                } else {
                    localStorage.setItem('theme', 'light');
                }
            });
            
            // 搜索按钮点击事件
            searchBtn.addEventListener('click', function() {
                adminStatus.textContent = '搜索中...';
                const teacher = teacherSearch.value.trim();
                const url = teacher ? `?teacher=${encodeURIComponent(teacher)}` : '';
                window.location.href = url;
            });
            
            // 清除搜索按钮点击事件
            clearSearchBtn.addEventListener('click', function() {
                adminStatus.textContent = '清除搜索...';
                teacherSearch.value = '';
                window.location.href = '';
            });
            
            // 回车键触发搜索
            teacherSearch.addEventListener('keypress', function(e) {
                if (e.key === 'Enter') {
                    searchBtn.click();
                }
            });
            
            // 模态框外部点击关闭
            window.onclick = function(event) {
                const modal = document.getElementById('editModal');
                if (event.target == modal) {
                    closeModal();
                }
            }
        });
        
        // 删除高相似度论文条目
        function deleteHighSimilarityPaper(id) {
            if (confirm('确定要删除这条高相似度论文数据吗？')) {
                const adminStatus = document.getElementById('admin-status');
                adminStatus.textContent = '删除中...';
                
                fetch('/admin/high_similarity/delete/' + id, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    }
                })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        alert('删除成功');
                        // 重新加载当前页面
                        location.reload();
                    } else {
                        alert('删除失败: ' + data.message);
                        adminStatus.textContent = '删除失败';
                    }
                })
                .catch(error => {
                    alert('删除请求失败: ' + error.message);
                    adminStatus.textContent = '删除请求失败';
                });
            }
        }
        
        // 编辑修改意见
        function editReviewComments(id, currentComments) {
            currentEditId = id;
            // 处理可能的undefined或null值
            const comments = currentComments || '';
            document.getElementById('reviewComments').value = comments;
            document.getElementById('editModal').style.display = 'block';
        }
        
        // 关闭模态框
        function closeModal() {
            document.getElementById('editModal').style.display = 'none';
            currentEditId = null;
        }
        
        // 保存修改意见
        function saveReviewComments() {
            const comments = document.getElementById('reviewComments').value;
            if (currentEditId !== null) {
                const adminStatus = document.getElementById('admin-status');
                adminStatus.textContent = '保存中...';
                
                fetch('/admin/high_similarity/update_comments/' + currentEditId, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({comments: comments})
                })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        alert('保存成功');
                        closeModal();
                        // 重新加载当前页面
                        location.reload();
                    } else {
                        alert('保存失败: ' + data.message);
                        adminStatus.textContent = '保存失败';
                    }
                })
                .catch(error => {
                    alert('保存请求失败: ' + error.message);
                    adminStatus.textContent = '保存请求失败';
                });
            }
        }
        
        // 跳转到指定页
        function goToPage(page) {
            const adminStatus = document.getElementById('admin-status');
            adminStatus.textContent = '页面跳转中...';
            
            const teacher = document.getElementById('teacher-search').value.trim();
            let url = '?page=' + page;
            if (teacher) {
                url += '&teacher=' + encodeURIComponent(teacher);
            }
            window.location.href = url;
        }
        
        // 跳转到指定页码
        function jumpToPage() {
            const pageInput = document.getElementById('page-input');
            const page = parseInt(pageInput.value);
            const totalPages = {{ total_pages or 1 }};
            
            if (isNaN(page) || page < 1 || page > totalPages) {
                alert('请输入有效的页码 (1-' + totalPages + ')');
                return;
            }
            
            goToPage(page);
        }
        
    </script>
</body>
</html>